MyBatis 菜鸟教程 2 单表操作

您所在的位置:网站首页 菜鸟教程 table MyBatis 菜鸟教程 2 单表操作

MyBatis 菜鸟教程 2 单表操作

#MyBatis 菜鸟教程 2 单表操作| 来源: 网络整理| 查看: 265

对单表进行CRUD操作

模型对象User

package com.jege.mybatis.mapper; /** * @author JE哥 * @email [email protected] * @description:单表 */ public class User { private Long id; private String name; private Integer age; public User() { } public User(String name, Integer age) { this.name = name; this.age = age; } public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } @Override public String toString() { return "User [id=" + id + ", name=" + name + ", age=" + age + "]"; } }

映射文件UserMapper.xml

DROP TABLE IF EXISTS `t_user`; CREATE TABLE IF NOT EXISTS `t_user` ( `id` bigint(11) NOT NULL AUTO_INCREMENT, `name` varchar(255) DEFAULT NULL, `age` int(11) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; insert into t_user(name,age) values(#{name},#{age}) select id,name,age from t_user where id=#{id} select id,name,age from t_user update t_user set name=#{name},age=#{age} where id=#{id} delete from t_user where id=#{id}

测试类SingleTableTest

package com.jege.mybatis; import java.io.Reader; import java.util.List; import org.apache.ibatis.io.Resources; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder; import org.junit.After; import org.junit.Before; import org.junit.BeforeClass; import org.junit.Test; import com.jege.mybatis.mapper.User; /** * @author JE哥 * @email [email protected] * @description:单表CRUD Test */ public class SingleTableTest { private static final String NAME_SPACE = "com.jege.mybatis.mapper.UserMapper"; private static SqlSessionFactory sqlSessionFactory = null; private SqlSession sqlSession = null; @BeforeClass public static void setUpBeforeClass() throws Exception { Reader reader = Resources.getResourceAsReader("mybatis-config.xml"); sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader); } @Before public void setUp() throws Exception { sqlSession = sqlSessionFactory.openSession(); sqlSession.insert(NAME_SPACE + ".dropTable"); sqlSession.insert(NAME_SPACE + ".createTable"); sqlSession.commit(); } // mybatis会通过mapper文件配置把保存后的主键放到当前对象的id里面 @Test public void insert() throws Exception { sqlSession = sqlSessionFactory.openSession(); User user = new User("je-ge", 22); System.out.println("保存之前:" + user); sqlSession.insert(NAME_SPACE + ".insert", user); sqlSession.commit(); System.out.println("保存之后:" + user); System.out.println("+++++++++++++++"); } @Test public void update() throws Exception { insert(); User user = sqlSession.selectOne(NAME_SPACE + ".findByKey", 1L); System.out.println("修改前:" + user); user.setName("JE-GE"); user.setAge(18); sqlSession.update(NAME_SPACE + ".update", user); sqlSession.commit(); user = sqlSession.selectOne(NAME_SPACE + ".findByKey", 1L); System.out.println("修改后:" + user); } @Test public void findByKey() throws Exception { User user = sqlSession.selectOne(NAME_SPACE + ".findByKey", 1L); System.out.println(user); } @Test public void findAll() throws Exception { List users = sqlSession.selectList(NAME_SPACE + ".findAll"); for (User user : users) { System.out.println(user); } } @Test public void delete() throws Exception { User user = sqlSession.selectOne(NAME_SPACE + ".findByKey", 1L); System.out.println("删除前:" + user); sqlSession.delete(NAME_SPACE + ".delete", 1L); sqlSession.commit(); user = sqlSession.selectOne(NAME_SPACE + ".findByKey", 1L); System.out.println("删除后:" + user); } @After public void tearDown() throws Exception { if (sqlSession != null) sqlSession.close(); } } 其他关联项目 MyBatis 菜鸟教程1-环境配置 http://blog.csdn.net/je_ge/article/details/53998874 源码地址

https://github.com/je-ge/mybatis

如果觉得我的文章或者代码对您有帮助,可以请我喝杯咖啡。 您的支持将鼓励我继续创作!谢谢! 微信打赏 支付宝打赏



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3